package net.huashitong.common.service;

import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.sys.entity.SysUser;
import com.sirdc.modules.sys.service.SysUserService;
import com.sirdc.modules.sys.util.SysUserUtils;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.common.entity.SysAnnouncement;
import net.huashitong.common.entity.SysAnnouncementRecord;
import net.huashitong.common.enums.NormalNums;
import net.huashitong.common.filter.SysAnnouncementFilter;
import net.huashitong.common.filter.SysAnnouncementRecordFilter;
import net.huashitong.util.UtilsTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AnnounceService {
    @Autowired
    private SysAnnouncementService sysAnnouncementService;
    @Autowired
    private SysAnnouncementRecordService sysAnnouncementRecordService;
    @Autowired
    private SysUserService sysUserService;
    public Message saveAnnoucement(SysAnnouncementFilter filter){
        Message msg = new Message();
        SysAnnouncement announcement = new SysAnnouncement();
        UtilsTools.copyPro(filter,announcement);
        announcement.setSender(SysUserUtils.getUserId());
        sysAnnouncementService.save(announcement);
        switch (filter.getMsgType()) {
            case NormalNums.USER:
                if (StringUtils.isNotBlank(filter.getUserIds())) {
                    if (filter.getUserIds().contains(",")){
                        //指定一批
                        String[] ids = filter.getUserIds().split(",");
                        int total = ids.length;
                        int insert = 0;
                        for (String id:ids){
                            if(StringUtils.isBlank(StringUtils.trimToEmpty(id))){
                                continue;
                            }
                            SysUser sysUser = sysUserService.getById(id);
                            if (ObjectUtils.isBlank(sysUser)){
                                continue;
                            }
                            //给有效的通告  无效的userid 忽视
                            SysAnnouncementRecord record = new SysAnnouncementRecord();
                            record.setAnntId(announcement.getSysId());
                            record.setUserId(sysUser.getSysId());
                            // 阅读状态自动设为0
                            sysAnnouncementRecordService.save(record);
                            insert++;
                        }
                        msg.setCode(UtilsTools.CODE);
                        msg.setMessage("成功插入"+insert+"条通告，失败"+(total-insert)+"条");
                        return msg;
                    } else { //指定单个
                        SysUser sysUser = sysUserService.getById(StringUtils.trimToEmpty(filter.getUserIds()));
                        if (ObjectUtils.isBlank(sysUser)){
                            //总共才一条
                            msg.setCode(UtilsTools.ERROR);
                            msg.setMessage("插入通告失败");
                            return msg;
                        }
                        SysAnnouncementRecord record = new SysAnnouncementRecord();
                        record.setAnntId(announcement.getSysId());
                        record.setUserId(sysUser.getSysId());
                        // 阅读状态自动设为0
                        sysAnnouncementRecordService.save(record);
                    }
                } else {
                    msg.setCode(UtilsTools.ERROR);
                    msg.setMessage("未设置指定的用户");
                    return msg;
                }
                break;
            case NormalNums.ALL:
                List<SysUser> users = sysUserService.query();
                List<SysAnnouncementRecord> records = new ArrayList<>();
                for (SysUser sysUser:users){
                    SysAnnouncementRecord record = new SysAnnouncementRecord();
                    record.setAnntId(announcement.getSysId());
                    record.setUserId(sysUser.getSysId());
                    // 阅读状态自动设为0
                    records.add(record);
                }
                sysAnnouncementRecordService.batchSaveObject(records);
                break;
            default:
                msg.setCode(UtilsTools.ERROR);
                msg.setMessage("通告对象类型错误");
                return msg;
        }
        msg.setCode(UtilsTools.CODE);
        return msg;
    }

    public Message deleteForce(List<String> ids){
        Message msg = new Message();
        for (String id:ids){
            SysAnnouncement announcement = sysAnnouncementService.getById(id);
            if(ObjectUtils.isBlank(announcement)){
                continue;
            }
            SysAnnouncementRecordFilter filter = new SysAnnouncementRecordFilter();
            filter.setAnntId(announcement.getSysId());
            List<SysAnnouncementRecord> records = sysAnnouncementRecordService.queryByFilter(filter);
            sysAnnouncementService.delete(announcement);
            sysAnnouncementRecordService.batchDeleteObject(records);
        }
        return msg;
    }


}
